﻿using System.Threading.Tasks;

namespace Sidvall.Net.Sms
{
    public class SmsClient : ISmsClient
    {
        #region Public Members

        public string WebAddress { get; set; }

        #region SendAsync

        public async Task SendAsync(SmsMessage message)
        {
            string flash, webAddress;

            if (message == null)
                throw new System.ArgumentNullException("message");
            if (string.IsNullOrWhiteSpace(message.MobileNumber))
                throw new System.ArgumentException("Missing mobile number");
            if (message.Flash)
                flash = "yes";
            else
                flash = "no";
            if (!string.IsNullOrWhiteSpace(message.ConnectionString))
                webAddress = message.ConnectionString;
            else if (!string.IsNullOrWhiteSpace(SmsSystemContext.Current.WebAddress))
                webAddress = SmsSystemContext.Current.WebAddress;
            else
                webAddress = this.WebAddress;
            var mobileNumber = message.MobileNumber.Replace("+", "00").Replace("-", "");
            var settings = new HttpClientSettings();
            var client = Sidvall.SystemContext.Current.HttpClientFactory.GetHttpClient(settings);
            webAddress = string.Format(webAddress, mobileNumber, System.Uri.EscapeDataString(message.Body), flash);
            var response = await client.GetAsync(webAddress).ConfigureAwait(false);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return "SmsClient - Sidvall.Net.Sms.SmsClient";
        }

        #endregion

        #endregion
        #region Constructors

        public SmsClient()
        {
        }
        public SmsClient(string webAddress)
        {
            this.WebAddress = webAddress;
        }

        #endregion
    }
}
